class Book(val title: String, val author: String, val isbn: String) {
  private var _borrowed: Boolean = false

  def borrowed: Boolean = _borrowed

  def borrow(): Unit = {
    _borrowed = true
  }

  def returnBook(): Unit = {
    _borrowed = false
  }
}

object Book {
  def apply(title: String, author: String, isbn: String): Book = {
    new Book(title, author, isbn)
  }

  def unapply(book: Book): Option[(String, String, String)] = {
    Some(book.title, book.author, book.isbn)
  }
}

class Library(private var books: List[Book] = Nil) {
  def addBook(book: Book): Unit = {
    books = book :: books
  }

  def borrowBook(isbn: String): Unit = {
    val index = findBookIndexByIsbn(isbn)
    if (index >= 0) {
      val book = books(index)
      if (!book.borrowed) {
        book.borrow()
        println(s"The book with ISBN $isbn is borrowed.")
      } else {
        println("The book is not available.")
      }
    } else {
      println("The book is not found.")
    }
  }

  def returnBook(isbn: String): Unit = {
    val index = findBookIndexByIsbn(isbn)
    if (index >= 0) {
      val book = books(index)
      if (book.borrowed) {
        book.returnBook()
        println(s"The book with ISBN $isbn is returned.")
      } else {
        println("The book is not borrowed from this library.")
      }
    } else {
      println("The book is not found.")
    }
  }

  private def findBookIndexByIsbn(isbn: String): Int = {
    var index = 0
    while (index < books.length && books(index).isbn != isbn) {
      index += 1
    }
    if (index < books.length) index else -1
  }
}

object Library {
  def apply(books: Book*): Library = {
    new Library(books.toList)
  }
}
